package com.chenguo.learn.demo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chenguo.learn.demo.domain.entity.UserEntity;
import com.chenguo.learn.demo.service.IUserService;
import com.chenguo.learn.demo.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @ClassName: UserController
 * @Author: chenguo
 * @Data: 2018年10月16日 17时15分
 * @Description:
 * @Company: 成都灵奇空间软件
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @RequestMapping(value = "/find/{id}")
    @ResponseBody
    public Result<UserEntity> findUserById(@PathVariable Integer id) {
        UserEntity userEntity = userService.getById(id);
        String json = JSON.toJSONString(userEntity);
//        return JSONObject.parseObject(json);
        return Result.success(userEntity);
    }

    @RequestMapping(value = "save")
    @ResponseBody
    public Result<String> save(UserEntity userEntity){
        userService.save(userEntity);
        return Result.success("操作成功");
    }

    @RequestMapping(value = "update")
    @ResponseBody
    public Result<String> update(UserEntity userEntity){
        userService.update(userEntity);
        return Result.success("操作成功");
    }

    @RequestMapping(value = "delete")
    @ResponseBody
    public Result<String> delete(UserEntity userEntity){
        userService.delete(userEntity);
        return Result.success("操作成功");
    }
}
